﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace Nemerle2.Compiler
{
/// <summary>
  /// Type name generators.
  /// </summary>
  public module ConverterHelper
  {  
      private ToString(fixedType : FixedType, flags : ConvertOption) : string
      {
          def ToStringShortcut(typ : NType)
          {
              | typ is FixedType => ToString(typ, flags)
              | _                => throw NotImplementedException("Unexpected type. Expected 'FixedType'. Got '" + typ.GetType().FullName + "'." );
          }
          
          match(fixedType)
          {
          | Class(info, [])           => info.Name
          | Class(info, args)         => $<#$info.Name[..$(args; ", "; ToStringShortcut)]#>
          | TypeParameterInfoRef(varRef)  => $<#!!$varRef#>
          | Fun(Tuple(args), to)      => $<#..$(args; " * ") -> $(ToStringShortcut(to))#>
          | Tuple(args)               => $<#..$(args; " * ")#>
          | Array(t, 1)               => $<#array[$(ToStringShortcut(t))]#>
          | Array(t, rank)            => $<#array[$rank, $(ToStringShortcut(t))]"#>
          | Ref(t)                    => $<#ref $(ToStringShortcut(t))#>
          | Out(t)                    => $<#out $(ToStringShortcut(t))#>
          | Void                      => "void"
          | Intersection(types)       => $<#INT: (..$(types; ", "))#>
          }
      }
      
      public ToString(param : IParameterInfo, flags : ConvertOption) : string
      {
          param.Name + " : " + ToString(param.Type, flags)
      }
  }
}
